////////////////////////////////////////////////////////////////
// Active Commentators for Blogger
// version: 4.0
// @2009 - 2012, minimus
// author: minimus
// author's blogs: http://blogcoding.ru, http://www.simplelib.com
////////////////////////////////////////////////////////////////
jQuery(document).ready(function($) {
  var blog;

  blog = new google.Blog(function() {
    blog.getCommentsJson(onLoadFeed);
  }, window.name);



  function onLoadFeed(data) {
    function oCommentator (name, url, img, num, ctx) {
    	this.name = name;
    	this.url = url;
      this.img = img;
    	this.num = num;
      this.ctx = ctx;
    }

    function doDrawButtonImg(idx) {
      var
        ctx = acList[idx].ctx,
        img = acList[idx].img;

      if (!$.browser.safari && !$.browser.mozilla)
        ctx.drawImage(img, 2,  2, 32, 32);
      else {
        if (img.complete) ctx.drawImage(img, 2, 2, 32, 32);
      }
    }

    function drawButton(acData, idx) {
      var ctx = acData.ctx;

      if (null == ctx) return;
      ctx.clearRect(0, 0, cw, ch);
      doDrawButtonImg(idx);
    }

    function makeImg(src, idx) {
      var img = new Image();
      //img.onload = new Function('doDrawButtonImg('+idx+');');
      img.src = src;

      return img;
    }

    var
      prefs = new gadgets.Prefs(),
      author = data.data.feed.author[0].name.$t,
      anonym = 'Anonymous',
      comms = data.data.feed.entry,
      blankImg = 'http://img1.blogblog.com/img/blank.gif',
      acBlankImg = 'http://active-commentators.googlecode.com/svn/trunk/mx-blank.png',
      bloggerImg = 'http://img2.blogblog.com/img/b16-rounded.gif',
      acBloggerImg = 'http://active-commentators.googlecode.com/svn/trunk/mx-blogger.png',
      options = {
        maxList : prefs.getInt('acmaxlist'),
        excludeAuthor: prefs.getBool('acexcludeauthor'),
        excludeAnonym: prefs.getBool('acexcludeanonym'),
        authorUrl : true,
        loadStr : 'Loading ...',
        errorStr : 'Error ...'
      },
      ac = new Array(), acSorted, acList = new Array(),
      canvas, cw, ch, ctx;

    if(undefined == data) $('#ac4').text(options.errorStr);
    else $('#ac4').text(options.loadStr);

    $.each(comms, function(index, comm) {
      var isNew = false;
      if(!((options.excludeAuthor && comm.author[0].name.$t == author) ||
          (options.excludeAnonym && comm.author[0].name.$t == anonym))) {
        var img = comm.author[0].gd$image.src;
        if(img == blankImg || img == bloggerImg) img = ((img == blankImg) ? acBlankImg : acBloggerImg);
        if(ac.length == 0)
          ac.push(new oCommentator(comm.author[0].name.$t, comm.author[0].uri.$t, makeImg(img, 0), 1, null));
        else {
          isNew = true;
          for(var i=0; i < ac.length; i++) {
            if((comm.author[0].name.$t == ac[i].name) && (comm.author[0].uri.$t == ac[i].url)) {
              isNew = false;
              ac[i].num++;
              break;
            }
            else isNew = true;
          }
          if(isNew)
            ac.push(new oCommentator(comm.author[0].name.$t, comm.author[0].uri.$t, makeImg(img, ac.length), 1, null));
        }
      }
    });
    acSorted = ac.sort(function(a, b) {return b.num - a.num});
    //var cwidth = $("#ac4").width();
    for(var j=0; (j < options.maxList) && (j < acSorted.length); j++) acList.push(acSorted[j]);
    $("#ac4").text('');
    for(var k=0; k < acList.length; k++) {
      /*if(!document.createElement('canvas').getContext) {

      }
      else {
        $("#ac4").text('').append('<canvas id="ac-canvas-' + k + '" width="' + cwidth + '" height="36"></canvas>');
        canvas = $("#ac-canvas-" + k).get(0);
        ch = canvas.height;
        cw = canvas.width;
        ctx = canvas.getContext('2d');
        acList[k].ctx = ctx;
        drawButton(acList[k], k);
      }*/
      var buttonHTML = '<div>'+
        '<a class="ac-link" target="_top" href="'+acList[k].url+'"><img id="ac-img-'+k+'" src="'+acList[k].img.src+'" /></a>'+
        '<div id="ac-name-'+k+'"><a class="ac-link" target="_top" href="'+acList[k].url+'">'+acList[k].name+'</a></div>'+
        '<div id="ac-comm-'+k+'">Комментариев: '+acList[k].num+'</div>' +
        '</div>';
      $("#ac4").append(buttonHTML);
      $("#ac-img-"+k).css({
        margin: '2px',
        float: 'left',
        width: '32px',
        height: '32px'
      });
      $("#ac-name-"+k).css({
        "font-size": '14px',
        "font-weight": 'bold',
        color: 'red',
        margin: '3px'
      });
      $("#ac-comm-"+k).css({
        "font-size": '11px',
        color: 'blue',
        margin: '3px'
      });
    }
    gadgets.window.adjustHeight();
  }
})(jQuery);